package com.dcd.service.impl;

import com.dcd.mapper.VideoMapper;
import com.dcd.pojo.Video;
import com.dcd.service.VideoService;
import com.dcd.util.SolrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private SolrUtil solrUtil;
    @Autowired(required = false)
    private VideoMapper videoMapper;

    /**
     * 查询所有视频排行榜
     *
     * @return 返回前十的视频
     */
    @Override
    public List<Video> select_All_video_ranking() {
        return videoMapper.select_All_video_ranking();
    }

    /**
     * 查询所有视频的节目
     *
     * @param pageVideoindex 起始页
     * @return 返回所有视频，节目名称,总数量
     */
    @Override
    public Map<String, Object> select_all_video_program(Integer pageVideoindex, Integer video_type_id) {
        Map<String, Object> map = new HashMap<>();
        Page<Object> page = PageHelper.startPage(pageVideoindex, 24);//56
        videoMapper.select_all_video_program(video_type_id);
        map.put("Pagedata", page.getResult());//数据
        map.put("Pageindex", page.getPageNum());//起始页
        map.put("PageTotal", page.getPages());//总页数
        return map;
    }

    /**
     * 根据车系id查询该车系所有视频
     *
     * @param car_series_id 车系id
     * @return
     */
    @Override
    public List<Video> select_series_id_video(Integer car_series_id) {
        return videoMapper.select_series_id_video(car_series_id);
    }

    /**
     * 查询最新的视频信息
     *
     * @return
     */
    @Override
    public List<Video> select_new_video_mess() {
        return videoMapper.select_new_video_mess();
    }

    /**
     * 查询指定节目下指定车系视频
     *
     * @param program_id    节目id
     * @param car_series_id 车系id
     * @param page_index    当前页数
     * @return
     */
    @Override
    public Map<String, Object> select_appoint_program_all_video(int program_id, int car_series_id, int page_index) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Video> page = PageHelper.startPage(page_index, 16);
        videoMapper.select_appoint_program_all_video(program_id, car_series_id);
        map.put("result", page);
        map.put("pages", page.getPages());
        return map;
    }

    /**
     * 搜索查询视频
     *
     * @param solr_content 搜索文本内容
     * @param page_index   当前页数
     * @return
     */
    @Override
    public Map<String, Object> select_video_by_solr(String solr_content, int page_index) throws IOException, SolrServerException {
        HashMap<String, Object> conditions = new HashMap<>();
        conditions.put("solr_content", solr_content);
        conditions.put("collection", "dcd_video");
        conditions.put("highlight_field", "video_introduction");
        conditions.put("page_index", page_index);
        conditions.put("all_fields", new String[]{"id", "video_id,video_images_url",
                "video_broadcast_count", "video_upload_date",
                "video_duration", "video_introduction",
                "user_head_portrait_url", "user_name", "user_head_portrait_url","user_id"});
        return solrUtil.solr_content_by_collection(conditions);
    }

    /**
     * 根据视频id 查询视频
     * 返回 视频名称，路径 简介，以及用户资料
     * @param video_id   视频id
     * @return
     */
    @Override
    public Video select_videoByID_video(Integer video_id) {
        //这里代表已经在播放页面了 需要修改视频播放次数
        videoMapper.update_video_count(video_id);
        return videoMapper.select_videoByID_video(video_id);
    }


    /**
     * 根据视频id 查询该节目所有视频
     * 返回 视频基本信息 / 节目名称
     * @param video_id
     * @return
     */
    @Override
    public List<Video> select_video_id_all(Integer video_id) {
        return videoMapper.select_video_id_all(video_id);
    }


}
